// const app = require('express')()

//导入uuid模块
const uuid = require('node-uuid')

//导入数据库操作模块
const db = require("../../db/index.js")

// 格式化时间
var moment = require('moment')

/**
 * 获取简介
 * @param {*} req 
 * @param {*} res 
 */
exports.getDescription = async (req, res) => {
    console.log('获取简介列表')
    const title = req.query.title
    console.log(title)
    const sql = 'SELECT * FROM xks_description  WHERE status != 0 AND  title = ?';
    await db.query(sql, title, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '错误' + err,
                data: {}
            })
        }
        // console.log(results)
        return res.send({
            code: 200,
            msg: '成功',
            data: results[0]
        })
    })
}


/**
 * 增加简介
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.addDescription = (req, res) => {
    const Description = req.body;
    if (JSON.stringify(Description) == "{}") {
        console.log('【增加简介发送错误】' + 'req.body ===> ', req.body)
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!Description.title || !Description.img_url || !Description.introduce) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }

    //该简介是否已存在
    const findSql = 'SELECT * FROM xks_Description  WHERE status != 0 AND  title = ?'
    db.query(findSql, Description.title, (err, results) => {
        if (err) {
            console.log(err.message)
            return res.send({
                code: 505,
                msg: err.message,
                data: {}
            })
        }
        if (results.length > 0) {
            return res.send({
                code: 400,
                msg: '该简介（标题）已存在，请重新操作！',
                data: {}
            })
        }


        const addSql = `INSERT INTO xks_Description SET ? `
        db.query(addSql, Description, (err, result) => {
            if (err) {
                console.log(err.message)
                return res.send({
                    code: 505,
                    msg: err.message,
                    data: {}
                })
            }
            if (result.affectedRows !== 1) {
                return res.send({
                    code: 500,
                    msg: '增加失败，请稍后再试！',
                    data: {}
                })
            }
            res.send({
                code: 200,
                msg: '增加成功',
                data: {}
            })
        })
    })
}


/**
 * 删除简介
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.delDescription = (req, res) => {

    var Description = req.params;
    const admin = req.data
    if (admin.level != 1) {
        return res.send({
            code: 403,
            msg: '没有权限',
            data: {}
        })
    }

    if (JSON.stringify(Description) == "{}") {
        console.log('【删除简介发送错误】' + 'req.body ===> ', req.body)
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!Description.id) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }

    const findSql = `SELECT * FROM xks_description  WHERE status != 0 AND  id = ?`;

    db.query(findSql, Description.id, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '发送了错误' + err.message,
                data: {}
            })
        }

        if (!results.length > 0) {
            return res.send({
                code: 400,
                msg: '没有找到该数据',
                data: {}
            })
        }

        Description = results[0]

        const delSql = `UPDATE xks_description SET status = 0 WHERE id = ? `
        db.query(delSql, Description.id, (err, result) => {
            if (err) {
                console.log(err.message)
                return res.send({
                    code: 505,
                    msg: err.message,
                    data: {}
                })
            }
            if (result.affectedRows !== 1) {
                return res.send({
                    code: 500,
                    msg: '删除失败，请检查ID是否有误！',
                    data: {}
                })
            }
            res.send({
                code: 200,
                msg: '删除成功！',
                data: {}
            })
        })
    });

}


/**
 * 更新简介
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.updateDescription = (req, res) => {
    const Description = req.body;
    if (JSON.stringify(Description) == "{}") {
        console.log('【更新简介发送错误】' + 'req.body ===> ', req.body)
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!Description.title || !Description.img_url || !Description.introduce) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }

    // 判断是否有操作的权限
    const admin = req.data
    if (admin.level == 3 && Description.title == '所况简介') {
        console.log('没有权限，admin.level ===> ' + admin.level)
        return res.send({
            code: 403,
            msg: '对不起，没有权限修改该内容！',
            data: {}
        })
    }

    //记录id的值
    const DescriptionId = Description.id
    delete Description.id

    const updateSql = `UPDATE xks_description SET ?  WHERE status != 0 AND  id = ?`
    db.query(updateSql, [Description, DescriptionId], (err, result) => {
        if (err) {
            console.log(err.message)
            return res.send({
                code: 505,
                msg: err.message,
                data: {}
            })
        }
        if (result.affectedRows !== 1) {
            return res.send({
                code: 500,
                msg: '更新失败，请稍后再试！',
                data: {}
            })
        }
        res.send({
            code: 200,
            msg: '更新成功',
            data: {}
        })
    })
}


